package com.live.dengdengtv.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.MainHallActivity;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfo;
import com.live.dengdengtv.bean.SimpleInfoMap;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.Notice;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;

public class BindPhoneActivity extends BaseActivity implements View.OnClickListener{


//    private Button verifyCode_bt;
    private EditText verify_et;
    private EditText phone_et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_phone);

        phone_et = (EditText) findViewById(R.id.phone_et);
        verify_et = (EditText) findViewById(R.id.verify_et);

//        verifyCode_bt = (Button) findViewById(R.id.verifyCode_bt);
        findViewById(R.id.title_left_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.verifyCode_bt:
                getVerifyCode();
                break;
            case R.id.next_bt:
                bindPhone();
                break;
        }

    }


    public void getVerifyCode() {

        OkHttpUtils.post().url(NetTaskList.getVerify)
                .addParams("token", AppInfo.TOKEN)
                .addParams("phone_type", AppInfo.PHONE_TYPE)
                .addParams("phone_system_version", AppInfo.getPhone_system_version())
                .addParams("phone_app_version", AppInfo.getPhone_app_version())
                .addParams("client_id", AppInfo.getClient_id())
//                .addParams("user_id", AppInfo.loginInfo.getUser_id())

                .addParams("phone", phone_et.getText().toString())
                .addParams("code_type", "4")
                .build()
                .execute(verifyCodeCallback);
    }

    private BaseActivity.BaseInfoCallback verifyCodeCallback = new BaseActivity.BaseInfoCallback(){

        @Override
        public void onError(Call call, Exception e, int id) {
            Notice.alert(context, "网络错误...");
        }
        @Override
        public void onResponse(SimpleInfo response, int id) {
            if(response.result == 0){
                Notice.alert(context, response.data);
            }
        }
    };

    private void bindPhone(){

        OkHttpUtils.post().url(NetTaskList.login_code)
                .addParams("token", AppInfo.TOKEN)
                .addParams("phone_type", AppInfo.PHONE_TYPE)
                .addParams("phone_system_version", AppInfo.getPhone_system_version())
                .addParams("phone_app_version", AppInfo.getPhone_app_version())
                .addParams("client_id", AppInfo.getClient_id())
                .addParams("user_id", AppInfo.loginInfo.getUserId())

                .addParams("account_type", "4")
//                .addParams("open_id", "")
                .addParams("phone", phone_et.getText().toString())
                .addParams("code", verify_et.getText().toString())
//                .addParams("account", "")
//                .addParams("image_url", "")
//                .addParams("nickname", "")
                .build()
                .execute(bindCallback);
    }

    private BaseActivity.BaseInfoMapCallback bindCallback = new BaseActivity.BaseInfoMapCallback(){
        @Override
        public void onError(Call call, Exception e, int id) {

            Notice.alert(context, "网络错误...");
        }
        @Override
        public void onResponse(SimpleInfoMap response, int id) {
            if(response.result == 0){
                AppInfo.loginInfo.setUserId(response.data.get("user_id"));
                AppInfo.loginInfo.setUserNickname(phone_et.getText().toString());
                AppInfo.loginInfo.holdLoginInfo(context);

                setResult(Activity.RESULT_OK);
                Notice.alertAndFinish(context, "绑定成功!");
            }else {
                Notice.alert(context, "验证码错误!");
            }
        }
    };

}
